package official;


import java.rmi.RemoteException;

public class TraiteurCommand implements Runnable{
	Thread leThread;
	String command;	
	int idJoueur;
	Server_Int leServer;	
	boolean continuer;
	boolean quitCommand;

	public TraiteurCommand(String command,int idJoueur,Server_Int leServer) {
		super();		
		this.leServer = leServer;
		this.idJoueur = idJoueur;
		this.command = command;
		this.continuer=false;
		this.quitCommand=false;		
	}
			
	public Server_Int getLeServer() {
		return leServer;
	}

	public void setLeServer(Server_Int leServer) {
		this.leServer = leServer;
	}

	public Thread getLeThread() {
		return leThread;
	}

	public void setLeThread(Thread leThread) {
		this.leThread = leThread;
	}

	public String getCommand() {
		return command;
	}

	public void setCommand(String command) {
		this.command = command;
	}

	public int getIdJoueur() {
		return idJoueur;
	}

	public void setIdJoueur(int idJoueur) {
		this.idJoueur = idJoueur;
	}

	public boolean isContinuer() {
		return continuer;
	}

	public void setContinuer(boolean continuer) {
		this.continuer = continuer;
	}

	public boolean isQuitCommand() {
		return quitCommand;
	}

	public void setQuitCommand(boolean quitCommand) {
		this.quitCommand = quitCommand;
	}

	public void start(){
		continuer=true;
		leThread = Thread.currentThread();
	}
	
	public void stop(){
		continuer=false;		
		if (leThread!=null) leThread.interrupt();
	}

	@Override
	public void run() {		
		// TODO Auto-generated method stub		
		
			char code = '\0';
			if (command!=null) code = command.charAt(0);		
			switch (code) {
	        	case 'm' :	        		
	        		//move n/s/e/o
	        		if (command.length()==3) {	        			
	        			try {
							leServer.deplacer(idJoueur, command.charAt(2));
						} catch (RemoteException e) {
							e.printStackTrace();
						}
	        		}
	        		else {
	        			System.out.println("Error : command m n[/s/o/e] to move");
	        		}
	        		break;
	        	case 's' :
	        		//dialoguer
	        		String mess=command.substring(2);
					try {
						leServer.dialoguer(idJoueur, mess);
					} catch (RemoteException e1) {
						e1.printStackTrace();
					}		          		
	        		break;
	        	case 'a' :
	        		//attack
	        		String idPersoCibleStr = command.substring(2);		          		
	        		int idPersoCible = Integer.parseInt(idPersoCibleStr);
					try {
						leServer.attaquePerso(idJoueur, idPersoCible);
					} catch (RemoteException e) {
						e.printStackTrace();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}		          		
	        		break;
	        	case 'r' :
	        		if (command.length()==1) {
	        			try {
							leServer.annulerCombat(idJoueur);
						} catch (RemoteException e) {
							e.printStackTrace();
						}
	        		}
	        		else {
	        			System.out.println("Error : Unknown command");
	        		}
	        		//run
	        		break;
	        	case 'q' :
	        		//do nothing
	        		break;
	        	default :
	        		System.out.println("Error : Unknown command : " + command);	        
	  	  }			
		this.stop();		
	}

}
